//
//  TagPageViewController.m
//  PetTown
//
//  Created by lanou3g on 15/5/30.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "TagPageViewController.h"
#import "TagPageCell.h"
#import "TagePageDefileTableViewController.h"
#import "UIImageView+WebCache.h"
#import "HomePageTableViewController.h"
@interface TagPageViewController ()
{
    MBProgressHUD * mbBar;
}
@end

@implementation TagPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController.navigationBar setTranslucent:NO];
#pragma mark------模糊效果
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"beijingmaoboli.png"]];
    //创建模糊视图对象
    UIVisualEffectView * visualEffectView = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    //给模糊视图设置frame
    visualEffectView.frame = self.view.bounds;
    //添加模糊视图
    [self.view addSubview:visualEffectView];

    [self readData];
    [self setBarButtonItem];//设置BarButtonItem
}
//设置BarButtonItem
-(void)setBarButtonItem{
    UIButton * retunButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    retunButton.frame = CGRectMake(0, 0, 32, 32);
    [retunButton setBackgroundImage:[UIImage imageNamed:nil] forState:UIControlStateNormal];
    [retunButton addTarget:self action:nil forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem * leftBI = [[UIBarButtonItem alloc] initWithCustomView:retunButton];
    self.navigationItem.leftBarButtonItem = leftBI;
    self.navigationItem.title = @"标签";
}
//读取数据
- (void)readData{
    mbBar = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:mbBar];
    mbBar.labelText = @"正在加载..";
    [mbBar show:YES];
    
    NSURL * url = [NSURL URLWithString:TagPageAPI];
    NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:600.0];
    
    self.tagArray = [NSMutableArray array];//创建model对象数组
    __weak TagPageViewController * tagPVC = self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (nil == data) {
            return ;
        }
        NSMutableDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray * array = [dic objectForKey:@"data"];
        for (NSMutableDictionary * diction in array) {
            TagPage * tagPage = [[TagPage alloc] init];
            [tagPage setValuesForKeysWithDictionary:diction];
            [tagPVC.tagArray addObject:tagPage];
        }
        
        [self.view reloadInputViews];
           [self setSubView];
    }];
    
}
//设置视图
- (void)setSubView{
    //创建集合视图样式
    UICollectionViewFlowLayout * collectionViewFL = [[UICollectionViewFlowLayout alloc] init];
    //设置item大小
    collectionViewFL.itemSize = CGSizeMake((CGRectGetWidth(self.view.frame)-52)/3, (CGRectGetWidth(self.view.frame)-52)/3 + 30);
    collectionViewFL.minimumLineSpacing = 10;
    collectionViewFL.sectionInset = UIEdgeInsetsMake(20, 10, 10, 10);
    //创建集合视图
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:collectionViewFL];
    collectionView.backgroundColor = [UIColor clearColor];
    //设置代理
    collectionView.dataSource = self;
    collectionView.delegate = self;
    //将集合视图添加到view上
    [self.view addSubview:collectionView];
    
    //设置几个视图的cell类型
    [collectionView registerClass:[TagPageCell class] forCellWithReuseIdentifier:@"TagePageCell"];

}
//设置collectionView显示的item的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _tagArray.count;
}
//设置cell显示内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TagPageCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TagePageCell" forIndexPath:indexPath];
    TagPage * tagPage = _tagArray[indexPath.item];
    cell.tagPage = tagPage;
    NSURL * url = [NSURL URLWithString:[tagPage cover]];
    cell.layer.borderColor = [UIColor colorWithRed:222.0/256.0 green:158.0/256.0 blue:126.0/256.0 alpha:1.0].CGColor;
    cell.layer.borderWidth = 2;
    [cell.imgView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"place.jpg"]];

    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    return cell;
}
//点击cell推出详情页面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TagePageDefileTableViewController * TagePDTVC = [[TagePageDefileTableViewController alloc] init];
    TagePDTVC.tag_id = [_tagArray[indexPath.item] tag_id];
    TagePDTVC.itemTitle = [_tagArray[indexPath.item] tag_name];
    [self.navigationController pushViewController:TagePDTVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    //清除缓存
    SDImageCache * sdI = [SDImageCache sharedImageCache];
    [sdI clearDisk];
    [sdI clearMemory];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
